package com.hzit.cart.controller;

import cn.hutool.crypto.SecureUtil;
import com.hzit.cart.api.model.ItemVo;
import com.hzit.cart.service.CartService;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Controller
@RequestMapping
public class CartController {
	@Autowired
	private CartService cartService;

	//1. 购物车列表页面
	@GetMapping({"/","/cart.html"})
	public String index(Model model){
		//1.1 获取购物车
		Map<String, List<ItemVo>> cartMap = cartService.getCartList();
		//1.2 放到model中
		model.addAttribute("cartMap",cartMap);
		return "cart";
	}

	//2. 到订单页
	@GetMapping("/cart/toTrade")
	public String toTrade(HttpSession session){
		//2.1 生成token放到redis中
		String token = cartService.createTokenToRedis();
		session.setAttribute("token",token);
		return "redirect:http://order.hzit.com";
	}

	// 查询购物车列表
	@GetMapping("/cart/findCart")
	@ResponseBody
	public R<Map<String,List<ItemVo>>> findCart(){
		Map<String, List<ItemVo>> cartList = cartService.getCartList();
		return R.ok(cartList);
	}
	/**
	 * RedirectAttributes attributes
	 * attributes.addFlashAttribute();将数据放在session里面可以在页面取出，但只能取一次
	 * attributes.addAttribute("skuId",skuId); 将数据放在url后面
	 */
	//2. 添加购物车,添加后到购物车页面显示列表
	@GetMapping("/cart/addCart/{id}/{num}")
	public String addCart(@PathVariable("id") String id,
	                      @PathVariable Integer num,
	                      RedirectAttributes  attributes,
	                      Model model) throws InvocationTargetException, IllegalAccessException {
		cartService.addCart(id,num);
		//2.1 从redis中取出当前登录用户的购物车列表信息(购物车列表)
		// key： 商家名字或商家id
		// value：商家对应的所有商品集合
		Map<String, List<ItemVo>> cartMap = cartService.getCartList();
		//2.3 放到model中
		model.addAttribute("cartMap",cartMap);
		//2.4 在请求后添加参数
		attributes.addAttribute("skuId",id);
		attributes.addAttribute("buyNum",num);
		//添加成功后，应该跳转到“成功加入到购物车页面”
		return "redirect:http://cart.hzit.com/cart/success.html";
	}

	//3. 修改购物车数量
	@GetMapping("/cart/updateNum/{id}/{num}")
	public String updateNum(  @PathVariable("id") String id,
		                      @PathVariable Integer num,
		                      Model model) throws InvocationTargetException, IllegalAccessException {
		//3.1 修改购物车数量
		cartService.addCart(id,num);
		//3.2 从redis中取出当前登录用户的购物车列表信息(购物车列表)
		// key： 商家名字或商家id
		// value：商家对应的所有商品集合
		Map<String, List<ItemVo>> cartMap = cartService.getCartList();
		//3.3 放到model中
		model.addAttribute("cartMap",cartMap);
		return "redirect:http://cart.hzit.com/";
	}


	//3. 到达成功添加到购物车页面
	@GetMapping("/cart/success.html")
	public String success(@RequestParam("skuId") String skuId,
	                      @RequestParam("buyNum") Integer buyNum,
	                      Model model) throws InvocationTargetException, IllegalAccessException {
		ItemVo itemVo = cartService.getItemById(skuId,buyNum);
		model.addAttribute("item",itemVo);
		return "success-cart.html";
	}

	//3. 从购物车中删除商品
	@GetMapping("/cart/removeCart/{id}/{sellerId}")
	public String removeCat(@PathVariable("id") String id,
	                        @PathVariable("sellerId") String sellerId,
	                        Model model) throws Exception {
		//3.2 从redis中删除购物车
		Map<String, List<ItemVo>> cartMap = cartService.removeCart(id,sellerId);
		model.addAttribute("cartMap",cartMap);
		return "cart";
	}
}
